/*
 * unittest2.c
 * make a set of random tests
 * to see cover as much of each function as possible.
 * This test will be for getWinners
 * I will exclude invalid input for now.
 * Help was from class examples, badTestDrawCard.c
 * and an example of ramdom testing from a previous class
 * found at http://code.google.com/p/cs362s2013/source/browse/trunk/lutzjo/dominion/unittest1.c
 *  Created on: Jan 25, 2014
 *      Author: Charles_OSU
 */


#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "rngs.h"
#include <math.h>

#define DEBUG 0

// prototypes
int scoreForTest(int player, struct gameState *state);


int scoreForTest (int player, struct gameState *state) {

  int i;
  int score = 0;
  int numErrors = 0;
  int result = 0;
  struct gameState gsAfter;
      memcpy(&gsAfter, state, sizeof(struct gameState));



  //score from hand
  for (i = 0; i < gsAfter.handCount[player]; i++)
    {
      if (gsAfter.hand[player][i] == curse) { score = score - 1; };
      if (gsAfter.hand[player][i] == estate) { score = score + 1; };
      if (gsAfter.hand[player][i] == duchy) { score = score + 3; };
      if (gsAfter.hand[player][i] == province) { score = score + 6; };
      if (gsAfter.hand[player][i] == great_hall) { score = score + 1; };
      if (gsAfter.hand[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, &gsAfter) / 10 ); };
    }

  //score from discard
  for (i = 0; i < gsAfter.discardCount[player]; i++)
    {
      if (gsAfter.discard[player][i] == curse) { score = score - 1; };
      if (gsAfter.discard[player][i] == estate) { score = score + 1; };
      if (gsAfter.discard[player][i] == duchy) { score = score + 3; };
      if (gsAfter.discard[player][i] == province) { score = score + 6; };
      if (gsAfter.discard[player][i] == great_hall) { score = score + 1; };
      if (gsAfter.discard[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, &gsAfter) / 10 ); };
    }

  //score from deck
  for (i = 0; i < gsAfter.discardCount[player]; i++)
    {
      if (gsAfter.deck[player][i] == curse) { score = score - 1; };
      if (gsAfter.deck[player][i] == estate) { score = score + 1; };
      if (gsAfter.deck[player][i] == duchy) { score = score + 3; };
      if (gsAfter.deck[player][i] == province) { score = score + 6; };
      if (gsAfter.deck[player][i] == great_hall) { score = score + 1; };
      if (gsAfter.deck[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, &gsAfter) / 10 ); };
    }

  result = scoreFor(player, state);

          if(result != score){
              printf("Our result of %d did not match our score of %d \n",result, score);
              printf("Our paramaters at the time were  gsAfter.handCount[player] = %d, gsAfter.discardCount[player] = %d and gsAfter.discardCount[player] = %d \n",  gsAfter.handCount[player], gsAfter.discardCount[player], gsAfter.discardCount[player]);
              numErrors += 1; //did not match, add one to the error
            }
            if(memcmp(&gsAfter, state, sizeof(struct gameState)) != 0){
              printf("Our gameStates do not match! \n");
              numErrors += 1; //Gamestates did not match so add one to numErrors
            }

            return numErrors;

}




int main() {

   // Create main game play setup including structs,
  // variables, and any state I need to emulate

  struct gameState gameStateTest;//testing gameStateTest

  int x, y, z;
//  int playersTest[MAX_PLAYERS];
  int players;  // players
 // int minPlayers=2;
  int maxPlayers=4;
  int numErrors = 0;
  int testNums = 0;
 // int k[10] = {adventurer, council_room, feast, gardens, mine,
 //	       remodel, smithy, village, baron, great_hall};

  // Set the random number of players
  //players = (int)floor(Random() * maxPlayers);

 // now seed the game
  SelectStream(2);
  PutSeed(3);


  // Print to our screen which tests are being run
  // which will get directed to out .out file later
  // when we add commands to the makefile to redirect
  // the output

  printf("Testing scoreForTest begin\n");
 // printf ("%d RANDOM TESTS.\n", testNums);

  // Additional setup and initialization. Some code
  // borrowed from badTestDrawCard.c

  for (x = 0; x < 3000; x++) {
      for (y = 0; y < sizeof(struct gameState); y++) {
           ((char*)&gameStateTest)[y] = floor(Random() * 256);
         }

      // Set the random number of players
       players = (int)floor(Random() * maxPlayers);

      // Setup my players decks and hands
       gameStateTest.deckCount[players] = floor(Random() * MAX_DECK);
       gameStateTest.discardCount[players] = floor(Random() * MAX_DECK);
       gameStateTest.handCount[players] = floor(Random() * MAX_HAND);



      for(z=0;z<gameStateTest.handCount[players];z++)
      {

    	  gameStateTest.hand[players][z] = (int)(Random() * MAX_HAND);

      }

      for(z=0;z<gameStateTest.discardCount[players];z++)
      	  {

         	  gameStateTest.discard[players][z] = (int)(Random() * MAX_HAND);

           }

      for(z=0; z<gameStateTest.deckCount[players]; z++)
          {

            gameStateTest.deck[players][z] = (int)(Random() * MAX_HAND);

          }


     // printf ("Initialization and setup completed now running tests for scoreForTest and counting errors.\n");
      numErrors += scoreForTest(players, &gameStateTest);
      testNums += 1;
    }


  // If we get to heere it means we kept running and did not crash the program
  // so our tests must have run now let's see how many errors we got.
    printf ("All Tests ran for scoreForTest\n");
    printf ("%d RANDOM TESTS.\n", testNums);



if (numErrors > 0) { printf("There were %d errors found for scoreForTest. ", numErrors);}
else { printf("There were no errors found in scoreForTest.");}


return 0;

}  // End main
